package com.cloudcc.boot.controller;

import com.cloudcc.boot.service.SapService;
import com.yangzg.developer.returned.result.GenReturnResultUtils;
import com.yangzg.developer.returned.result.ReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RestController
@RequestMapping(path = "/api")
public class CrmToSapController {

    private final SapService sapService;

    public CrmToSapController(SapService sapService) {
        this.sapService = sapService;
    }

    @GetMapping(value = "/pushOrderToSap")
    public ReturnResult pushOrderToSap(@RequestParam("orderId") String orderId) {
        log.debug("推送订单 - {} 到SAP", orderId);
        try {
            sapService.pushOrderToSap(orderId);
            log.debug("推送订单 - {} 到SAP成功", orderId);
            return GenReturnResultUtils.genSuccessResult();
        } catch (Exception e) {
            log.error("推送订单 - {} 到SAP失败: {}", orderId, e.getMessage());
            return GenReturnResultUtils.genFailedResult(e, true);
        }
    }


}
